package net.springmvc11_12.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        // 1加载mybatis的核心配置文件
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("mybatis.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 2 利用工厂建造类 创建工厂对象
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static  SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
